﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data.Car_Rental;
using Common.Background.Web.Controllers;
using Entity.Car_Rental;
using Entity.CommonEnums;

namespace Background.Controllers
{
    [ValidateInput(false)]
    public class NewsController : BackgroundBaseController
    {
        IHelperCenterCategoryRepository iHelperCenterCategoryRepository = null;
        INewsRepository iNewsRepository = null;
        public NewsController()
        {
            iHelperCenterCategoryRepository = new HelperCenterCategoryRepository();
            iNewsRepository = new NewsRepository();
            this.ValidateRequest = false;
        }
        //
        // GET: /News/

        public ActionResult Index(int? page)
        {
            VP = new Entity.VPredication();
            PP = new Entity.PagingParam(page ?? 1, base.PageSize);
            var model = iNewsRepository.GetNews(VP, PP);

            return View(model);
        }

        //
        // GET: /News/Details/5

        public ActionResult Details(int? id)
        {
            if (!id.HasValue)
                return RedirectToAction("Index");
            var model = iNewsRepository.GetModelForBackground().FirstOrDefault(i => i.NewsID == id);
            return View(model);
        }

        //
        // GET: /News/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /News/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            News entity = new News();
            TryUpdateModel(entity);
            entity.CreateDate = DateTime.Now;
            entity.UpdateDate = DateTime.Now;
            entity.Status = (int)Status.Normal;
            if (entity.IsValid)
            {
                iNewsRepository.Insert(entity);
                return RedirectToAction("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }
            return View();
        }

        //
        // GET: /News/Edit/5

        public ActionResult Edit(int? id)
        {
            if (!id.HasValue)
                return RedirectToAction("Index");
            var model = iNewsRepository.GetModelForBackground().FirstOrDefault(i => i.NewsID == id.Value);
            return View(model);
        }

        //
        // POST: /News/Edit/5

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            News entity = new News();
            TryUpdateModel(entity);
            entity.UpdateDate = DateTime.Now;
            if (entity.IsValid)
            {
                iNewsRepository.Update(entity);
                return AlertToUrl("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }

            return View();
        }

        //
        // GET: /News/Delete/5

        public ActionResult Delete(int? id)
        {
            if (!id.HasValue)
                return RedirectToAction("Index");
            iNewsRepository.Delete(new News { NewsID = id.Value });
            return AlertToUrl("Index");
        }


    }
}
